package org.example.springboot.controller;

import org.example.springboot.entity.FinanceApplication;
import org.example.springboot.repository.FinanceApplicationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class FinanceApplicationController {

    @Autowired
    private FinanceApplicationRepository repository;

    // 用户申请融资
    @PostMapping("/apply")
    public FinanceApplication apply(@RequestBody FinanceApplication app) {
        app.setStatus("PENDING");
        app.setApplyTime(LocalDateTime.now());
        return repository.save(app);
    }

    // 管理端：获取所有融资申请
    @GetMapping("/all")
    public List<FinanceApplication> all() {
        return repository.findAll();
    }

    // 管理端：同意融资
    @PostMapping("/approve/{id}")
    public FinanceApplication approve(@PathVariable Long id) {
        FinanceApplication app = repository.findById(id).orElseThrow();
        app.setStatus("APPROVED");
        app.setApproveTime(LocalDateTime.now());
        return repository.save(app);
    }
} 